package com.rpc.protocol;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import io.netty.handler.codec.MessageToMessageEncoder;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;

import java.util.List;

/**
 * @author yanzhuoleo
 * @version V1.0
 * @Description: 编码器
 * @date 2018-01-16-下午7:14
 */
@RequiredArgsConstructor
public class RpcEncode extends MessageToByteEncoder {

    private ISerialize iSerialize = new ProtostuffSerializeImpl();
    @NonNull
    private Class objectClass;


    @Override
    protected void encode(ChannelHandlerContext ctx, Object msg, ByteBuf out) {
        if (objectClass.isInstance(msg)) {
            byte[] data = iSerialize.serialize(msg);
            out.writeInt(data.length);
            out.writeBytes(data);
        }
    }
}
